package com.alienlab.utils;
import java.io.File;
import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;


/**
 * 包括
 * @author 赵刚
 *
 */
public class NewsJsoup {
	private Properties propertyConfig = new PropertyConfig("jdbc.properties").getProperties();
	private String news_url=  propertyConfig.getProperty("url");
	private String dummy_path=  propertyConfig.getProperty("dummy-path");
	private String news_name;//报纸的名称
	private String url;//报纸的地址
	private String choose_char;//报纸的条件字符
	private String need_char;//所有需要的字符
	HttpServletRequest request;
	public NewsJsoup(String news_name, String url, String choose_char, String need_char) {
		super();
		this.news_name = news_name;
		this.url = url;
		this.choose_char = choose_char;
		this.need_char = need_char;
	}
	
	public NewsJsoup() {
		super();
	}

	public String getNews_name() {
		return news_name;
	}
	
	public void setNews_name(String news_name) {
		this.news_name = news_name;
	}
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	
	public String getChoose_char() {
		return choose_char;
	}
	
	public void setChoose_char(String choose_char) {
		this.choose_char = choose_char;
	}
	
	public String getNeed_char() {
		return need_char;
	}
	
	public void setNeed_char(String need_char) {
		this.need_char = need_char;
	}
	
	/**
	 * 根据url地址,标签选择得到本网站所有的pdf文件
	 * @param getpath 
	 * @return list
	 * @throws IOException
	 */
	
	public JSONArray getNeedFile(String url,String name, String getpath) throws IOException{
		//创建链接，得到html文本对象
		long start=System.currentTimeMillis();
		
		Document doc = Jsoup.connect(url).get();
		//文本选择
        Elements links = doc.select("a[href$=.pdf]");
        JSONArray jsonArray = new JSONArray();
		JSONArray jsonArrayhtml = new JSONArray();
		String today = NowTime.getNowTime("yyyy-MM-dd");
		File file = new File(""+getpath+"\\"+today+"");
		if(file.exists()){ 
		}else{
			file.mkdir();
		}
		Set<String> set = new TreeSet<>();
        for (Element link : links) {
        	//先把地址按顺序放到set集合中
        	set.add(print("%s", link.attr("abs:href"), trim(link.text(), 35)));	
        }	
        Iterator<String> iterator = set.iterator();
        List<String> list = new ArrayList<>();
        while(iterator.hasNext()){
        	list.add(iterator.next());
        }
        int listlen = list.size();
        for(int i = 0 ; i < listlen ; i ++){
        		HttpDownload.download(list.get(i),""+getpath+"\\"+today+"\\"+today+"-"+name+"-"+i+".pdf");
            	jsonArray.add("" + dummy_path + "" + today + "/" + today + "-" + name + "-" + i + ".pdf");
			System.out.println(""+news_url+""+today+"/"+today+"-"+name+"-"+i+"/"+today+"-"+name+"-"+i+".html");
			    jsonArrayhtml.add(""+news_url+""+today+"/"+today+"-"+name+"-"+i+"/"+today+"-"+name+"-"+i+".html");
        }
        if("999".equals(name)){
        	
        }else {
        	NewsJsoup newsJsoup = new NewsJsoup();
            if(jsonArray.size()==0){
            	
            }else{
            	newsJsoup.postdata(name,jsonArrayhtml);
            }
		}
        long end=System.currentTimeMillis();
		return jsonArray;
	}
	
	public JSONArray getNeedFile_(String url,String name, String getpath)throws IOException{
		//创建链接，得到html文本对象
		Document doc =  Jsoup.connect(url).get();

		//文本选择
        Elements links = doc.select("a[href$=.pdf]");
        JSONArray jsonArray = new JSONArray();
        String today = NowTime.getNowTime("yyyy-MM-dd");
		File file = new File(""+getpath+"\\"+today+"");
		if(file.exists()){ 
		}else{
			file.mkdir();
		}
		Set<String> set = new TreeSet<>();
        for (Element link : links) {
        	//先把地址按顺序放到set集合中
        	set.add(print("%s", link.attr("abs:href"), trim(link.text(), 35)));	
        }	
        Iterator<String> iterator = set.iterator();
        List<String> list = new ArrayList<>();
        while(iterator.hasNext()){
        	list.add(iterator.next());
        }
        int listlen = list.size();
        for(int i = 0 ; i < listlen ; i ++){
            	jsonArray.add(list.get(i));
        }
		return jsonArray;
	}

	public JSONArray getUrlPdf(List<String> name,List<String> url, String getpath){
		NewsJsoup newsJsoup = new NewsJsoup();
		String today = NowTime.getNowTime("yyyy-MM/dd");
		int len = name.size();
		JSONArray jsonArray_all = new JSONArray();
		for(int i = 0; i<len ; i++){	
			JSONArray jsonArray = new JSONArray();
			try {
				String news_url = url.get(i).replace("$$",today);
				jsonArray = newsJsoup.getNeedFile(news_url,name.get(i),getpath);
			} catch (IOException e) {	
				System.err.println("网址格式错误！");
			}
			jsonArray_all.add(jsonArray);
		}	
		return jsonArray_all;
	}
	
	
	public void postdata(String name , JSONArray jsonArray){
		String today = NowTime.getNowTime("yyyy-MM/dd");
		String str = "{\"news_no\":"+name+",\"news_time\":\""+today+"\",\"news_pdf_url\":"+jsonArray+"}";
		try {
			String poString = HttpUtil.getInstance().post("https://leancloud.cn:443/1.1/classes/news_pdf",str);
			System.out.println(poString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
    //格式化方法
    private static String print(String msg, Object... args) {
        return String.format(msg, args);
    }
    
    //如果过长用点代替
    private static String trim(String s, int width) {
        if (s.length() > width)
            return s.substring(0, width-1) + ".";
        else
            return s;
    }
    
    @Override
	public String toString() {
		return "NewsJsoup [news_name=" + news_name + ", url=" + url + ", choose_char=" + choose_char + ", need_char="
				+ need_char + "]";
	}  
}
